package com.ruoyi.video.service;

import com.ruoyi.common.utils.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class VideoProcessorService {

    @Value("${video.process.url}")
    private String processUrl;
    private String getServerUrl() {
        return processUrl + "/process-video?url=URL&user_id=USER_ID&video_id=VIDEO_ID&PARAMS";
    }

    @Value("${video.regpotin}")
    private String regPoint;

    public Long getRegPoint(){
        if(StringUtils.isEmpty(regPoint)) return 0l;
        return Long.valueOf(regPoint);
    }

    @Value("${product.api.full}")
    private String productFullUrl;

    public String getProductFullUrl() {
        return productFullUrl;
    }

    @Async
    public void processVideo(Long userId, Long videoId, String videoPathOrUrl, String params) {

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(getServerUrl().replaceAll( "URL", videoPathOrUrl )
                    .replaceAll( "VIDEO_ID", videoId.toString() )
                    .replaceAll( "USER_ID", userId.toString() )
                    .replaceAll( "PARAMS", params )
            );
            System.out.println(httpGet.getUri());
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                int statusCode = response.getCode();
                String responseBody = EntityUtils.toString(response.getEntity());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}